﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Motoball.screenmanager;

namespace Motoball
{
    public class BasicModel
    {
        public string drawSetId = "default";
        public Model model { get; protected set; }
        protected Matrix world = Matrix.Identity;
        protected Matrix[] transforms;
        
        
        public Dictionary<string, Vector3> diffColors;

        public BasicModel(Model m)
        {
            model = m;
            transforms = new Matrix[model.Bones.Count];
            
            diffColors = new Dictionary<string, Vector3>(model.Meshes.Count);

            foreach (ModelMesh mesh in model.Meshes)
            {
                
                diffColors.Add(mesh.Name, Vector3.One);
                foreach (BasicEffect be in mesh.Effects)
                {
                    be.EnableDefaultLighting();
                }
            }
        }

        
        public virtual void Draw(Camera camera)
        {
            if (drawSetId == GameScreen.screenManager.currentDrawSetId)
            {
                model.Root.Transform = World;
                model.CopyAbsoluteBoneTransformsTo(transforms);
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect be in mesh.Effects)
                    {
                        be.GraphicsDevice.RenderState.DepthBufferEnable = true;
                        //be.EnableDefaultLighting();
                        be.Projection = camera.projection;
                        be.View = camera.view;
                        be.World = transforms[mesh.ParentBone.Index];
                    }
                    mesh.Draw();
                    
                }
            }
        }

        public Matrix World
        {
            get { return world; }
            set { world = value; }
        }

    }

    
}

